package com.zxj.userscenter.common.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zxj.userscenter.common.controller.BaseController;
import com.zxj.userscenter.common.util.ConstatFinalUtil;
import com.zxj.userscenter.common.util.EncryptUtil;
import com.zxj.userscenter.users.pojo.AUsers;
import com.zxj.userscenter.users.pojo.AUsersEnum;
import com.zxj.userscenter.users.service.IUsersService;

@Controller
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;

	/**
	 * 打开登陆的页面
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.LOGGER.info("-----login----");
		HttpSession session = request.getSession();
		/* 如果session有管理员，说明已经登陆 */
		AUsers users = (AUsers) session.getAttribute("users");
		if (users != null)
		{
			/* 已经登陆 */
			if (this.returnUrl(request, response, users))
			{
				return null;
			} else
			{
				/* 登陆成功 */
				return "redirect:/back/admins/main.htm";
			}
		}
		return "/head/login";
	}

	/*登陆提交*/
	@RequestMapping("loginSubmit")
	public String loginSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.LOGGER.info("loginSubmit");
		HttpSession session=request.getSession();
		
		/*接受参数*/
		String email=request.getParameter("email");
		String password=request.getParameter("password");
		String rand=request.getParameter("rand");
		
		/*从session取出验证码图片的验证*/
		String randSess=session.getAttribute("rand")+"";
		
		/*对比验证码
		 * 万能验证码
		 * */
		if((randSess!=null&&randSess.equalsIgnoreCase(rand))||"0715".equalsIgnoreCase(rand))
		{
			/*根据邮箱查询管理员*/
			Map<String,Object> condMap=new HashMap<String,Object>();
			condMap.put("email", email);
			AUsers users=this.usersService.findOneUsersService(condMap);
			/*用户存在*/
			if(users!=null)
			{
				/*验证密码*/
				if(EncryptUtil.checkPass(password, users.getPassword()))
				{
					/*密码正确*/
					if(users.getStatus()==AUsersEnum.STATUS_ENABLE.getStatus())
					{
						/*启用*/
						/*放入session*/
						session.setAttribute("users", users);
						session.setAttribute("lastLoginTime", users.getLastLoginTime());
					    
						/*修改管理员信息*/
						users.setLastLoginTime(new Date());
						this.usersService.updateOneUsersService(users);
						ConstatFinalUtil.LOGGER.info("登陆成功;id:{},邮箱:{},登陆时间:{}",users.getId(),users.getEmail(),
								users.getLastLoginTime().toLocaleString());
						if(this.returnUrl(request, response, users))
						{
							return null;
						}else
						{
							/*登陆成功*/
							return "redirect:/head/users/mian.thm";
						}
					}else
					{
						/*禁用
						 * 4:账户已禁用,请联系管理员
						 * */
						this.info=ConstatFinalUtil.INFO_JSON.getString("4");
					}
				}
			}
		}
		return null;
		
	}
	private boolean returnUrl(HttpServletRequest request, HttpServletResponse response, AUsers users)
	{
		return false;
	}
}
